#!/usr/bin/env python

import cmd
import sys
import random

from search import Search
from colors import * #@UnusedWildImport

def banner():
    quotes = ["It\'s not the figures lying, it\'s the liars figuring",
              "Statistics can be made to prove anything even the truth",
              "Facts are stubborn things, statistics are more pliable",
              "Fate laughs at probabilities",
              "There are three kinds of lies: Lies, Damn Lies, and Statistics"]
    print bold+"\n    [Buffer]Overflow proudly presents...."+W
    print "\t ,___________________________________________     "
    print "\t |\\'-._(   /                                 |   "
    print "\t | \\  .'-._\\  "+bold+"- Shelob Web Spider -"+W+"     ,   ,|  "
    print "\t |-.\\'    .-;     Version: v0.3.1     .'\\`-' |  "
    print "\t |   \\  .' (                       _.'   \\   |  "
    print "\t |.--.\\'   _)  By: Moloch,      ;-;       \\._|  "
    print "\t |    ` _\\(_)/_RedShift & f47h3r \\ `'-,_,-'\\ | "
    print "\t |______ /(O)\\  _________________/____)_`-._\\|  "
    quote = quotes[random.randint(0, len(quotes)-1)]
    print '\n    "'+quote+'"\n'
   
class ShelobUi(cmd.Cmd):

    intro = banner()
    prompt = underline+bold+'shelob'+W+bold+' > '+W
    doc_header = 'Spider'
    undoc_header = 'Other'
    search = Search()
    plugin = None
   
    def do_start(self, string):
        '''[*] Starts the web spider '''
        sys.stdout.write(bold+C+"[*]"+W+" Starting spider, please wait...\n"+W)
        self.plugin.execute()
   
    def do_exploit(self, string):
        '''[*] Starts the web spider '''
        self.do_start(string)
   
    def do_load(self, module):
        if 0 < len(module):
            try:
                mod = __import__(module)
                self.plugin = mod.loadPlugin()
                self.plugin.splash()
                self.prompt = underline+bold+'shelob'+W+bold+R+' ('+module+')'+W+bold+' > '+W
            except:
                print bold+R+'[!]'+W+' Error: Failed to load %s plugin.' % module, W
                self.plugin = None
        else:
            print bold+R+'[!] PEBKAC: See "help"' % module, W
   
    def do_save(self, fileName):
        '''[*] Save current settings to a file '''
        if len(fileName) == 0:
            fileName = 'shelob-settings.pkl'
        self.plugin.save(fileName)
   
    def do_show(self, option):
        if option == 'options':
            pramas = self.plugin.parameters
            index = 1
            for setting in pramas:
                
                print bold+C+"[%d]" % index, W+bold+setting+W+" - "+pramas[setting]
                index+=1
                  
    def do_set(self, input):
        option, value = input.split(' ')
        if option in self.plugin.parameters.keys() and 0 < len(value):
            self.plugin.set(option, value)
            print bold+C+'[*]'+W, option+' -> '+ value
        else:
            print bold+R+'[!] PEBKAC: %s is not a valid setting, see "show options"' % option, W

    def do_about(self, string):
        ''' [*] Display the Shelob banner '''
        banner()
        
    def do_search(self, string):
        '''[*] Search known Google Hacks with keywords'''
        results = self.search.find(string)
        for result in results:
            print result
                            
    def do_exit(self, string):
        '''[*] Exit the console interface '''
        print bold+'[*] Have a wonderful day!' +W
        sys.exit()

if __name__ == '__main__':
    try:
        ShelobUi().cmdloop()
    except KeyboardInterrupt:
        print '\b\b  \n[*] User exit, have a nice day!'


